package com.bw.zcb.mvp.presenter;

import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.bw.zcb.mvp.base.model.BaseEntity;
import com.bw.zcb.mvp.base.presenter.BasePresenter;
import com.bw.zcb.mvp.contract.LoginContract;
import com.bw.zcb.mvp.model.entity.ResponseUserEntity;
import com.bw.zcb.mvp.model.entity.UserEntity;
import com.bw.zcb.network.tool.App;
import com.bw.zcb.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginPresenter extends BasePresenter<LoginContract.ILoginModel,LoginContract.ILoginView> {
    @Inject
    public LoginPresenter(LoginContract.ILoginModel iLoginModel, LoginContract.ILoginView iLoginView) {
        super(iLoginModel, iLoginView);
    }
    //声明对应请求方法
    //phone->当前用户登录的手机号
    //pwd->用户密码
    public void requestLogin(long phone,String pwd,int code) {
        if (pwd.isEmpty()) {
            ToastUtils.showMsg("当前密码为空");
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("pwd", pwd);
        map.put("code", code);
        m.request(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }
    @Override
    public void onNext(BaseEntity baseEntity) {
        if (baseEntity instanceof ResponseUserEntity){
            ResponseUserEntity responseUserEntity = (ResponseUserEntity) baseEntity;

            App.getInstance().getDaoSesson().insert(responseUserEntity.getValues());
        }
        v.refreshView(baseEntity);
    }
}
